<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\adminapi\controller\general;

use app\admin\model\Admin;
use app\common\controller\BackendApi;
use fast\Random;
use think\Validate;

/**
 * 个人配置
 *
 * @icon fa fa-user
 */
class Profile extends BackendApi
{

    protected $searchFields = 'id,title';

    /**
     * 查看
     */
    /*public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            $this->model = model('AdminLog');
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->where($where)
                ->where('admin_id', $this->auth->id)
                ->order($sort, $order)
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }*/

    public function detail()
    {
        $this->success('获取成功',$this->auth->getAdminInfo());
    }

    /**
     * 更新个人信息
     */
    public function update()
    {
        if(!$this->request->isPost()){
            $this->error('请求方式错误');
        }
        $params = $this->request->post();
        $params = array_filter(array_intersect_key(
            $params,
            array_flip(array('email', 'nickname', 'password', 'avatar'))
        ));
        unset($v);
        if (!Validate::is($params['email'], "email")) {
            $this->error(__("Please input correct email"));
        }
        if (isset($params['password'])) {
            if (!Validate::is($params['password'], "/^[\S]{6,30}$/")) {
                $this->error(__("Please input correct password"));
            }
            $params['salt'] = Random::alnum();
            $params['password'] = md5(md5($params['password']) . $params['salt']);
        }
        $exist = Admin::where('email', $params['email'])->where('id', '<>', $this->auth->id)->find();
        if ($exist) {
            $this->error(__("Email already exists"));
        }
        if ($params) {
            $admin = Admin::get($this->auth->id);
            $admin->save($params);
            $this->success();
        }
        $this->error();
    }
}
